package greendroid.widget;

import greendroid.widget.QuickAction;
import greendroid.widget.QuickActionWidget;

import java.util.List;

import android.content.Context;
import android.graphics.Rect;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

import com.cyrilmottier.android.greendroid.R;

public class QuickActionText extends QuickActionWidget {
	
	private TextView mTextView;
	
	public QuickActionText(Context context) {
		super(context);
		setContentView(R.layout.gd_quick_action_text);
		
		final View v = getContentView();
		mTextView = (TextView) v.findViewById(R.id.gdi_text);
	}

	@Override
	protected void populateQuickActions(List<QuickAction> quickActions) {
		
		if(quickActions != null && quickActions.size() > 0) {
			
			QuickAction quickAction = quickActions.get(0);
			String mTitle = quickAction.mTitle.toString();
			
			mTextView.setText(mTitle);
		}
	}

	@Override
	protected void onMeasureAndLayout(Rect anchorRect, View contentView) {

        contentView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        contentView.measure(MeasureSpec.makeMeasureSpec(getScreenWidth(), MeasureSpec.EXACTLY),
                LayoutParams.WRAP_CONTENT);

        int rootHeight = contentView.getMeasuredHeight();

        int offsetY = getArrowOffsetY();
        int dyTop = anchorRect.top;
        int dyBottom = getScreenHeight() - anchorRect.bottom;

        boolean onTop = (dyTop > dyBottom);
        int popupY = (onTop) ? anchorRect.top - rootHeight + offsetY : anchorRect.bottom - offsetY;

        setWidgetSpecs(popupY, onTop);
    }

}
